package 剑指offer_leetcode;

import java.util.Arrays;

/**
 * JZ7重建二叉树
 */
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
 }


public class 剑指Offer07_重建二叉树 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        int m = inorder.length;
        if(n==0 || m==0)
            return null;
        TreeNode root = new TreeNode(preorder[0]);
        for(int i=0;i<m;i++){
            if(preorder[0]==inorder[i]){
                root.left = buildTree(Arrays.copyOfRange(preorder,1,i+1),Arrays.copyOfRange(inorder,0,i+1));
                root.right = buildTree(Arrays.copyOfRange(preorder,i+1,n),Arrays.copyOfRange(inorder,i+1,m));
                break;
            }
        }
        return root;
    }
}
